package com.majie.algorithm.linked_list;

import com.majie.algorithm.common.List;

public class CircleList<E> implements List<E> {

    int size;

    Node<E> node;

    Node<E> next;

    // 冗余,解决约瑟夫问题
    Node<E> current;

    int currentIndex;

    public void reset( ) {
        this.current = node;
        this.currentIndex = 0;
    }

    public E remove( ) {
        E element;
        if (this.currentIndex == 0) {
            Node<E> node = node(this.currentIndex);
            element = node.element;
            Node<E> last = node(size - 1);
            this.node = node.next;
            last.next = this.node;
        } else {
            Node<E> prev = node(this.currentIndex - 1);
            Node<E> next = prev.next;
            element = next.element;
            prev.next = next.next;
        }

        size--;
        if (this.currentIndex >= size) {
            this.currentIndex = 0;
        }
        return element;
    }

    public E next( ) {
        if (this.current == null) {
            return null;
        }
        //如果超过下标了还原
        if (this.currentIndex + 1 >= size) {
            this.currentIndex = 0;
        } else {
            this.currentIndex++;
        }
        this.current = this.current.next;
        return this.current.element;
    }

    @Override
    public void clear( ) {
        this.size = 0;
        this.node = null;
        this.next = null;
    }

    @Override
    public int size( ) {
        return this.size;
    }

    @Override
    public boolean isEmpty( ) {
        return this.size == 0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element) > 0;
    }

    @Override
    public void add(E element) {
        add(size, element);
    }

    @Override
    public E get(int index) {
        return null;
    }

    @Override
    public E set(int index, E element) {
        return null;
    }

    @Override
    public void add(int index, E element) {
        Node<E> node = new Node<>(element, null);
        if (index == 0) {
            this.node = node;
            this.next = node;
            this.node.next = this.next;
        } else {
            Node<E> prev = node(index - 1);
            node.next = prev.next;
            prev.next = node;
        }
        size++;
    }

    private Node<E> node(int index) {
        rangeCheckForAdd(index);
        int count = 0;
        Node<E> node = this.node;
        while (count < index) {
            node = node.next;
            count++;
        }
        return node;
    }

    private void rangeCheckForAdd(int index) {
        if (index < 0 || index >= size) {
            outOfBounds(index);
        }
    }

    private void outOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
    }

    @Override
    public E remove(int index) {
        rangeCheckForAdd(index);
        E element;
        if (index == 0) {
            Node<E> node = node(index);
            element = node.element;
            Node<E> last = node(size - 1);
            this.node = node.next;
            last.next = this.node;
        } else {
            Node<E> prev = node(index - 1);
            Node<E> next = prev.next;
            element = next.element;
            prev.next = next.next;
        }

        size--;
        return element;
    }

    @Override
    public int indexOf(E element) {
        return 0;
    }

    private static class Node<E> {
        E element;
        Node<E> next;

        Node(E element, Node<E> next) {
            this.element = element;
            this.next = next;
        }

        @Override
        public String toString( ) {
            StringBuilder sb = new StringBuilder();

            sb.append(element).append("_");

            if (next != null) {
                sb.append(next.element);
            } else {
                sb.append("null");
            }

            return sb.toString();
        }
    }


    @Override
    public String toString( ) {
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append(", [");
        Node<E> node = this.node;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }

            string.append(node);

            node = node.next;
        }
        string.append("]");
        return string.toString();
    }
}
